package com.googlecode.scenemaker3d.controller
{
	import com.googlecode.scenemaker3d.model.VO_Object3D;
	import com.googlecode.scenemaker3d.model.World3DModel;
	
	import mx.collections.ArrayCollection;
	
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.objects.DisplayObject3D;
	
	import com.googlecode.scenemaker3d.services.World;
	
	import com.googlecode.scenemaker3d.view.timeline.KeyFrame;
	import com.googlecode.scenemaker3d.view.timeline.Timeline;
	
	public class ObjectController implements IObjectController
	{
		private var mWorld:World3DModel;
		
		[Bindable]
		private var kFrames:ArrayCollection;
		
		public function ObjectController(m:World3DModel)
		{
			mWorld = m;
		}
		public function addObject(obj:DisplayObject3D):VO_Object3D
		{
			var VO:VO_Object3D = new VO_Object3D();	
			var name:String = obj.name +"_"+ String(mWorld.objID);
			
			var time:uint = mWorld.currentTimeInt;
			var kFrame:KeyFrame = new KeyFrame(Timeline.SQUARE_WIDTH, "keyFrame" + mWorld.keyFrameID);
			
			kFrame.enableDragging = false;
			kFrame.object3D = VO;
			kFrame.objPos = new Number3D(obj.x, obj.y, obj.z);
			
			obj.name = name;
			
			VO.id = name;
			VO.label = name;
			
			VO.data = obj;
			VO.keyFrames.addItem(kFrame);
			
			var objects:ArrayCollection = mWorld.objects;
			
			objects.addItem(VO);
			
			World.instance.scene.addChild(obj);
			
			mWorld.objID += 1;
			mWorld.keyFrameID +=1;	

			if(name.substr(0, 6) != "camera")
			{
				setSelectedObject(VO.id);
			}

			return VO;
		}
		public function removeObject(id:String):void
		{
			var objects:ArrayCollection = mWorld.objects;
			
			for(var i:int=0; i<objects.length; i++){
				if(objects.getItemAt(i).id == id){
					//Remove from scene
					World.instance.scene.removeChildByName(id);
					objects.removeItemAt(i);
					break;
				}
			}
		}
		public function getObjectAt(index:uint):VO_Object3D
		{
			var objects:ArrayCollection = mWorld.objects;
			
			return (objects.getItemAt(index) as VO_Object3D);
		}
		public function getObjectByName(id:String):VO_Object3D
		{
			var objects:ArrayCollection = mWorld.objects;
			
			for(var i:int=0; i<objects.length; i++){
				if(objects.getItemAt(i).id == id){
					return (objects.getItemAt(i) as VO_Object3D);
				}
			}
			return null;
		}
		public function getObjectListByType(type:String):Array
		{
			var objects:ArrayCollection = mWorld.objects;
			var objectList:Array = new Array();
			
			for(var i:int=0; i<objects.length; i++)
			{
				var stub:String = String(objects.getItemAt(i).id).split("_")[0];
				if(stub == type)
				{
					objectList.push(objects.getItemAt(i));
				}
			}
			return objectList;
		}
		public function setSelectedObject(id:String):void
		{
			if(id == null)
			{
				mWorld.selectedObject = null;
				
				return;
			}
			var VO:VO_Object3D = this.getObjectByName(id);
			mWorld.selectedObject = VO;
		}
		public function setObjectPos(id:String, pos:Number3D):void
		{
			//Prevent position being set if it has not changed
			var oldPos:Number3D = mWorld.selectedObjectPos;
			
			if(oldPos){
				if(oldPos.x == pos.x && oldPos.y == pos.y && oldPos.z == pos.z)
				{
					return;
				}
			}
			
			var VO:VO_Object3D = this.getObjectByName(id);
			
			VO.data.x = pos.x;
			VO.data.y = pos.y;
			VO.data.z = pos.z;
			
			mWorld.selectedObjectPos = pos;
		}
	}
}